<template>
  <div style="padding: 0px 100px;" class="flex">
    <div class="btn-group auto">
      <button type="button" class="btn btn-primary" @click="removeData(ids)">批量删除</button>
      <button type="button" class="btn btn-primary" @click='$goto("edit", {data: data})'>策略授权</button>
      <button type="button" class="btn btn-primary" @click='add("add-role")'>添加角色</button>
      <button type="button" class="btn btn-primary" @click='add("add-user")'>添加用户</button>
    </div>
    <resource-display :resource="data"></resource-display>

    <!-- <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div partial='default' class="auto"></div>
      </criteria> -->
    <div class="span">
      <data-grid :model='model'>
        <template partial='head'>
          <tr>
            <th colspan="5">部门用户管理</th>
          </tr>
          <tr>
            <th>全选<input type="checkbox" class="all-checked" v-model="checked" @click="$parent.$parent.isAll(checked)"></th>
            <th></th>
            <th>资源名称</th>
            <th>修改</th>
            <th>删除</th>
          </tr>
        </template>
        <template partial='body'>
          <td><input type="checkbox"  v-model="$parent.$parent.ids" :value="row"></td>
          <td><img :src="$parent.$parent.imgsrc(row.f_icon)" alt=""></td>
          <td>{{row.name}}</td>
          <td><span class="glyphicon glyphicon-edit" @click="$parent.$parent.modify(row)"></span></td>
          <td><span class="glyphicon glyphicon-remove" @click="$parent.$parent.removeData([row])"></span></td>
        </template>
      </data-grid>
    </div>
    <!-- </criteria-paged> -->
  </div>
</template>

<script>
  import { PagedList } from 'vue-client'
  import co from 'co'
  import * as Util from '../Util'
  import * as service from '../../service/LicenseService'
  import ResourceDisplay from "./ResourceDisplay";

  let gen = function * (self) {
    //self.model = yield self.$post('rs/search', {source: `tool.getChildren($${self.data.id}$)`, userid: self.userid},{resolveMsg: null, rejectMsg: null})
    self.model = yield self.$http.post('rs/search',{source: `tool.getChildren($${self.data.id}$).where(true)`, userid: self.userid})
    // console.log(self.model)
  }
  let deleteData = function * (self,data) {
    let msg = yield self.$http.post('rs/data/delete',{data: data})
    //yield self.$post('rs/data/delete', {data: data},{resolveMsg:null, rejectMsg:null})
    yield self.$showMessage(msg.bodyText)
    self.refresh()

  }
  export default {
    components: {ResourceDisplay},
    title: '部门',
    props: ['data', 'userid'],
    ready() {
      this.$parent.setTitle(this.data.name)
      this.refresh()
    },
    data() {
      return {
        ids: [], // 处理全选和部分选择使用
        model: '',
      }
    },
    methods: {
      // 刷新数据
      refresh () {
        let g = gen(this)
        co(g)
      },
      add(name) {
        console.log(name)
        let parent = this.data
        delete parent.children
        this.$goto(name, {parent: parent}, 'self', this.isRefresh)
      },
      modify(row) {
        let type = row.resourcetype
        let name = ''
        if (type === 'organization') {
          name = 'add-org'
        }else if (type === 'user') {
          name = 'add-user'
        }else if (type === 'role') {
          name = 'add-role'
        }
        let parent = this.data
        delete parent.children
        this.$goto(name, {parent: parent, data: row},'self', this.isRefresh)
        self.refresh()
      },
      isAll (checked) {
        this.ids = []
        if (!checked) {
          this.model.data.forEach((row) => {
            this.ids.push(row)
          })
        }
      },
      imgsrc (name) {
        return Util.getImgsrc(name)
      },
      gotoEdit () {
        this.$goto('edit', {data: this.data})
      },
      removeData (data) {
        let d = deleteData(this,data)
        co(d)
      },
      isRefresh (param) {
        if (param === 'refresh') {
          this.refresh()
        }
      }
    }
  }
</script>
<style>
  .all-checked {
    vertical-align: text-top;
    margin-left: 10px !important;
  }
  td > .glyphicon {
    cursor: pointer;
  }
  th > input[type="checkbox"], td > input[type="checkbox"] {
    cursor: pointer;
  }
</style>
